<?php
namespace Api\Controller;
use Think\Controller;

/**
 * Class PublicController  对外API
 * @package Api\Controller
 */
class PublicController extends CommonController{
//class PublicController extends Controller{

	//防止表单提交token
	function getToken(){
		$token = new \Think\Token();
		$sub_token = $token->set();
		return $sub_token;
	}

	//检查token
	function checkToken($sub_token){
		$token = new \Think\Token();
		//防止重复提交
		$result = $token->check($sub_token);
		if(!$result){
			return false;
		}
		return true;
	}

    //检查邀请码
    function checkInviteCode(){
        $u_model = D('User');
        $code = I('invitecode');
        $uid = $u_model->getUidByShareCode($code);
        if(is_int($uid)){
            $return['status'] = 1;
            $return['msg'] = '请求成功';
            //$return['data'] = $uid;
            $this->ajaxReturn($return, 'JSON');
        }else{
            $return['status'] = 0;
            $return['msg'] = '邀请人不存在';
            //$return['data'] = $uid;
            $this->ajaxReturn($return, 'JSON');        
        }
    }

    //重置密码
    function resetPwd(){
        $_post = I('post.');
        $result = check_sms($_post['mobile'], $_post['code'], 'mmbg', false);
        if(!$result['status']){
            $this->ajaxErrReturn($result['msg']);
        }
        $model = D('User');
        $result = $model->resetPwd($_post);
        if(!$result['status']){
            $this->ajaxErrReturn($result['msg']);
        }
        $this->ajaxSucReturn('修改成功');
    }

    /**
     * 图片上传
     */
    function upfile() {
        $data = $this->upload();
        foreach($data['url'] as $key=>$val){
            $list[$key] = $this->getPicUrl($val);
        }
        $this->ajaxSucReturn($list);
    }

    //退出
    function logout(){
        $result = [
            'status' => false,
            'data' => [],
            'msg' => ''
        ];
        if (!I("token")) {
            $this->ajaxErrReturn('出错');
        }
        $userTokenModel = D('Token');
        $re = $userTokenModel->delToken(I("token")); 
        if (!$re['status']) {
            return $re;
        }
        $this->ajaxSucReturn($re);
    }




}